<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Irregular Verb Conjugator</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="../classics.css" rel="stylesheet" type="text/css">
</head>

<?php

$endings = array();
function addEnding($type, $voice, $mood, $tense, $vI, $vthou, $vhe, $vwe, $vye, $vthey) {
	global $endings;
	$endings[$type][ $voice][$mood][$tense] = array('I' => $vI, 'thou' => $vthou, 'he' => $vhe,
		                                            'we' => $vwe, 'ye' => $vye, 'they' => $vthey);
}


addEnding('ferre', 'active', 'indicative', 'present', 'ō', 's', 't', 'imus', 'tis', 'unt');
addEnding('ferre', 'passive', 'indicative', 'present', 'or', 'ris|re', 'tur', 'imur', 'iminī', 'untur');
addEnding('ferre', 'active', 'subjunctive', 'present', 'am', 'ās', 'at', 'āmus', 'ātis', 'ant');
addEnding('ferre', 'passive', 'subjunctive', 'present', 'ar', 'āris|āre', 'ātur', 'āmur', 'āminī', 'antur');

addEnding('ferre', 'active', 'indicative', 'imperfect', 'ēbam', 'ēbās', 'ēbat', 'ēbāmus', 'ēbātis', 'ēbant');
addEnding('ferre', 'passive', 'indicative', 'imperfect', 'ēbar', 'ēbāris|ēbāre', 'ēbātur', 'ēbāmur', 'ēbāminī', 'ēbantur');
addEnding('ferre', 'active', 'subjunctive', 'imperfect', 'rem', 'rēs', 'ret', 'rēmus', 'rētis', 'rent');
addEnding('ferre', 'passive', 'subjunctive', 'imperfect', 'rer', 'rēris|rēre', 'rētur', 'rēmur', 'rēminī', 'rentur');

addEnding('ferre', 'active', 'indicative', 'future', 'am', 'ēs', 'et', 'ēmus', 'ētis', 'ent');
addEnding('ferre', 'passive', 'indicative', 'future', 'ar', 'ēris|ēre', 'ētur', 'ēmur', 'ēminī', 'entur');

addEnding('ferre', 'active', 'indicative', 'perfect', 'ī', 'istī', 'it', 'imus', 'istis', 'ērunt|ēre');
addEnding('ferre', 'passive', 'indicative', 'perfect', 'us sum', 'us es', 'us est', 'ī sumus', 'ī estis', 'ī sunt');
addEnding('ferre', 'active', 'subjunctive', 'perfect', 'erim', 'eris', 'erit', 'erimus', 'eritis', 'erint');
addEnding('ferre', 'passive', 'subjunctive', 'perfect', 'us sim', 'us sīs', 'us sit', 'ī sīmus', 'ī sītis', 'ī sint');

addEnding('ferre', 'active', 'indicative', 'pluperfect', 'eram', 'erās', 'erat', 'erāmus', 'erātis', 'erant');
addEnding('ferre', 'passive', 'indicative', 'pluperfect', 'us eram', 'us erās', 'us erat', 'ī erāmus', 'ī erātis', 'ī erant');
addEnding('ferre', 'active', 'subjunctive', 'pluperfect', 'issem', 'issēs', 'isset', 'issēmus', 'issētis', 'issent');
addEnding('ferre', 'passive', 'subjunctive', 'pluperfect', 'us essem', 'us essēs', 'us esset', 'ī essēmus', 'ī essētis', 'ī essent');

addEnding('ferre', 'active', 'indicative', 'future perfect', 'erō', 'eris', 'erit', 'erimus', 'eritis', 'erint');
addEnding('ferre', 'passive', 'indicative', 'future perfect', 'us erō', 'us eris', 'us erit', 'ī erimus', 'ī eritis', 'ī erunt');

$endings['ferre']['active']['imperative']['present']['thou'] = '';
$endings['ferre']['passive']['imperative']['present']['thou'] = 're';
$endings['ferre']['active']['imperative']['present']['ye'] = 'te';
$endings['ferre']['passive']['imperative']['present']['ye'] = 'iminī';
$endings['ferre']['active']['imperative']['future']['thou'] = 'tō';
$endings['ferre']['passive']['imperative']['future']['thou'] = 'tor';
$endings['ferre']['active']['imperative']['future']['ye'] = 'tōte';
$endings['ferre']['active']['imperative']['future']['he'] = 'tō';
$endings['ferre']['passive']['imperative']['future']['he'] = 'tor';
$endings['ferre']['active']['imperative']['future']['they'] = 'untō';
$endings['ferre']['passive']['imperative']['future']['they'] = 'untor';

$endings['ferre']['active']['infinitive']['present'] = 're';
$endings['ferre']['passive']['infinitive']['present'] = 'rī';
$endings['ferre']['active']['infinitive']['perfect'] = 'isse';
$endings['ferre']['passive']['infinitive']['perfect'] = 'us esse';
$endings['ferre']['active']['infinitive']['future'] = 'ūrus esse';
$endings['ferre']['passive']['infinitive']['future'] = 'um īrī';

$endings['ferre']['participle']['present'] = 'ēns';
$endings['ferre']['participle']['future'] = 'ūrus';
$endings['ferre']['participle']['perfect'] = 'us';
$endings['ferre']['participle']['gerundive'] = 'endus';

$endings['ferre']['gerund']['genitive'] = 'endī';
$endings['ferre']['gerund']['dative'] = 'endō';
$endings['ferre']['gerund']['accusative'] = 'endum';
$endings['ferre']['gerund']['ablative'] = 'endō';

$endings['ferre']['supine']['accusative'] = 'um';
$endings['ferre']['supine']['ablative'] = 'ū';


addEnding('esse', 'active', 'indicative', 'present', 'sum', 'es', 'est', 'sumus', 'estis', 'sunt');
addEnding('esse', 'active', 'subjunctive', 'present', 'sim', 'sīs', 'sit', 'sīmus', 'sītis', 'sint');

addEnding('esse', 'active', 'indicative', 'imperfect', 'eram', 'erās', 'erat', 'erāmus', 'erātis', 'erant');
addEnding('esse', 'active', 'subjunctive', 'imperfect', 'essem', 'essēs', 'esset', 'essēmus', 'essētis', 'essent');

addEnding('esse', 'active', 'indicative', 'future', 'erō', 'eris', 'erit', 'erimus', 'eritis', 'erunt');

addEnding('esse', 'active', 'indicative', 'perfect', 'ī', 'istī', 'it', 'imus', 'istis', 'ērunt|ēre');
addEnding('esse', 'active', 'subjunctive', 'perfect', 'erim', 'eris', 'erit', 'erimus', 'eritis', 'erint');

addEnding('esse', 'active', 'indicative', 'pluperfect', 'eram', 'erās', 'erat', 'erāmus', 'erātis', 'erant');
addEnding('esse', 'active', 'subjunctive', 'pluperfect', 'issem', 'issēs', 'isset', 'issēmus', 'issētis', 'issent');

addEnding('esse', 'active', 'indicative', 'future perfect', 'erō', 'eris', 'erit', 'erimus', 'eritis', 'erint');

$endings['esse']['active']['imperative']['present']['thou'] = 'es';
$endings['esse']['active']['imperative']['present']['ye'] = 'este';
$endings['esse']['active']['imperative']['future']['thou'] = 'estō';
$endings['esse']['active']['imperative']['future']['ye'] = 'estōte';
$endings['esse']['active']['imperative']['future']['he'] = 'estō';
$endings['esse']['active']['imperative']['future']['they'] = 'suntō';

$endings['esse']['active']['infinitive']['present'] = 'esse';
$endings['esse']['active']['infinitive']['perfect'] = 'isse';
$endings['esse']['active']['infinitive']['future'] = 'ūrus esse';

$endings['esse']['active']['participle']['future'] = 'ūrus';



function validateForm() {
	return ($_SERVER['REQUEST_METHOD'] == 'POST') && $_POST['verb'] && $_POST['meaning'] && 
	       ($_POST['pres_stem'] || $_POST['perf_stem'] || $_POST['part_stem']);
}


$defaults['conjugation'] = $endings[0];
$defaults['verb'] = '';
$defaults['meaning'] = '';
$defaults['pres_stem'] = '';
$defaults['perf_stem'] = '';
$defaults['part_stem'] = '';

if (validateForm()) {
	$defaults['conjugation'] = $_POST['conjugation'];
	$defaults['verb'] = $_POST['verb'];
	$defaults['meaning'] = $_POST['meaning'];
	$defaults['pres_stem'] = $_POST['pres_stem'];
	$defaults['perf_stem'] = $_POST['perf_stem'];
	$defaults['part_stem'] = $_POST['part_stem'];
}
?>

<script type="text/javascript">
function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
</script>

<body>
<div id="container">
<div id="center">
<form action="<?php echo $_SERVER['SCRIPT_NAME'] ?>" method="post">
<table align="center">
<caption>Latin Irregular Verb Conjugator</caption>
<tr class="separate-above">
<th>Type</th>
<td>
<select name="conjugation">
<?php

foreach($endings as $conj => $aConj) {
	print "<option value=\"$conj\"";
	if ($conj == $_POST['conjugation']) {
		print " SELECTED";
	}
	print ">$conj</option>\n";
}
?>
</select>
</td>
</tr>
<tr><th>Verb</th><td><input name="verb" type="text" value="<?php echo $defaults['verb']; ?>"></td></tr>
<tr><th>Meaning</th><td><input name="meaning" type="text" value="<?php echo $defaults['meaning']; ?>"></td></tr>
<tr><th>Present Stem</th><td><input name="pres_stem" type="text" value="<?php echo $defaults['pres_stem']; ?>"></td></tr>
<tr><th>Perfect Stem</th><td><input name="perf_stem" type="text" value="<?php echo $defaults['perf_stem']; ?>"></td></tr>
<tr><th>participle Stem</th><td><input name="part_stem" type="text" value="<?php echo $defaults['part_stem']; ?>"></td></tr>
<tr class="separate-above"><td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>



</div>
</div>
<?php

if (validateForm()) {
	$sql = "BEGIN;\n";
	foreach($endings[$_POST['conjugation']] as $voice => $aVoice) {
		if ($voice != 'active' && $voice != 'passive') {
			$words = array();

			foreach($aVoice as $tense => $ending) {
				$stem = $_POST['pres_stem'];
				if ($voice == 'supine' || ($voice == 'participle' && in_array($tense, array('perfect', 'future')))) {
					$stem = $_POST['part_stem'];
				}
				if ($stem) {
					$tmp = $stem;
					if ($_POST['conjugation'] == 'esse') {
						if ($stem == 'pot' && preg_match('/^s/', $ending)) {
							$tmp = 'pos';
						} elseif ($stem == 'prō' && preg_match('/^e/', $ending)) {
							$tmp = 'prōd';
						}
					}
					$words[] = "'$tmp$ending'";
				} else {
					$words[] = "NULL";
				}
			}

			$sql .= "INSERT INTO $voice VALUES('".$_POST['verb']."', ".implode(", ", $words).", '".$_POST['conjugation']."');\n";
			continue;
		}
		foreach(array('indicative', 'subjunctive', 'imperative') as $mood) {
			foreach($endings[$_POST['conjugation']][$voice][$mood] as $tense => $aTense) {
				$stem = "";
				if ($mood == 'imperative' || in_array($tense, array('present', 'imperfect', 'future')) ) {
					$stem = $_POST['pres_stem'];
				} elseif (in_array($tense, array('perfect', 'pluperfect', 'future perfect'))) {
					if ($voice == 'active') {
						$stem = $_POST['perf_stem'];
					} elseif ($voice == 'passive') {
						$stem = $_POST['part_stem'];
					}
				}
				
				if (! $stem) {
					continue;
				}
				$sql .= "INSERT INTO verbs VALUES('".$_POST['verb']."', '".$_POST['meaning']."', ";
				$sql .= "'$voice', '$mood', '$tense', ";
				$words = array();
				foreach (array('I', 'thou', 'he', 'we', 'ye', 'they') as $person) {
					if (isset($aTense[$person])) {
						$tmp = explode('|', $aTense[$person]);
						$tmpw = array();
						foreach($tmp as $ending) {
							$tmp = $stem;
							if ($_POST['conjugation'] == 'esse') {
								if ($stem == 'pot' && preg_match('/^s/', $ending)) {
									$tmp = 'pos';
								} elseif ($stem == 'prō' && preg_match('/^e/', $ending)) {
									$tmp = 'prōd';
								}
							}
							$tmpw[] = "$tmp$ending";
						}
						$words[$person] = "'".implode('|', $tmpw)."'";
					} else {
						$words[$person] = 'NULL';
					}
				}
				$sql .= implode(', ', $words).", '".$_POST['conjugation']."');\n";
			}
		}
		
		if (isset($aVoice['infinitive'])) {
			$sql .= "INSERT INTO infinitive VALUES('".$_POST['verb']."', '$voice', ";
			$words = array();
			foreach(array('present', 'future', 'perfect') as $tense) {
				$stem = "";
				if ($tense == 'present') {
					$stem = $_POST['pres_stem'];
				} elseif ( ($tense == 'perfect') && ($voice == 'active')) {
					$stem = $_POST['perf_stem'];
				} else {
					$stem = $_POST['part_stem'];
				}
				
				if ($stem && isset($aVoice['infinitive'][$tense])) {
					$tmp = explode('|', $aVoice['infinitive'][$tense]);
					$tmpw = array();
					foreach($tmp as $ending) {
						$tmp = $stem;
						if ($_POST['conjugation'] == 'esse') {
							if ($stem == 'pot' && preg_match('/^s/', $ending)) {
								$tmp = 'pos';
							} elseif ($stem == 'prō' && preg_match('/^e/', $ending)) {
								$tmp = 'prōd';
							}
						}
						$tmpw[] = "$tmp$ending";
					}
					$words[$tense] = "'".implode('|', $tmpw)."'";
				} else {
					$words[$tense] = 'NULL';
				}
			}
			$sql .= implode(', ', $words).", '".$_POST['conjugation']."');\n";
		}
	}
	
	$sql .= "COMMIT;\n";
?>
<hr />
<div id="container">
<div id="center">

<textarea align="center" style='font-size:8pt;' cols="120" rows="10" wrap="off" id="sqls" onClick="SelectAll('sqls');">
<?php echo $sql; ?>
</textarea>
</div>
</div>
<?php
}
?>
</body>
</html>
